package com.hitqz.robot.biz.controller;

import com.google.common.util.concurrent.RateLimiter;
import com.hitqz.robot.api.business.dto.RobotEquipmentControlButtonDto;
import com.hitqz.robot.api.business.util.ExcelUtil;
import com.hitqz.robot.biz.service.ButtonService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author xupkun
 * @date 2024/6/15
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("/ctrlEquipment")
@Tag(name = "外设控制")
@Slf4j
public class CtrlEquipmentController {

    private final ButtonService buttonService;


    private final RateLimiter limiter = RateLimiter.create(5);

    @Operation(summary = "按钮控制")
    @PostMapping("/button" )
    public void button(@RequestBody RobotEquipmentControlButtonDto button, HttpServletResponse response) throws IOException {
        if (limiter.tryAcquire(1)){
            log.info("web button: {}", button.getAction());
            buttonService.handleEquipmentButton(button,response);
        }
        ExcelUtil.response2Web(response,"指令下发成功");
    }








}
